package com;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
@WebServlet(name = "LoginServlet2" ,value = "/LoginServlet2")
public class LoginServlet2 extends HttpServlet {
            static String token = String.valueOf(System.currentTimeMillis()+new Random().nextInt(999999999));
            static Map<String,String> map=new HashMap<>();
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        String username=String.valueOf(request.getParameter("username"));
        String token1=(String) request.getParameter("token");
        if (username!=null&&map.get(username)==null){
            HttpSession session= request.getSession();
            map.put(username,token);
            session.setAttribute("token",map.get(username));
            System.out.println(session.getAttribute("token"));
            Cookie cookie=new Cookie("token", (String) session.getAttribute("token"));
            cookie.setPath(request.getContextPath()+"/login2.jsp");
            cookie.setMaxAge(60*60);
            response.addCookie(cookie);
            String uuu="欢迎"+String.valueOf(request.getParameter("username"))+"使用XXX系统";
            response.getWriter().println("<h1 style=\"color:red;text-align:center\">"+uuu+"</h1>");
        } else if (username!=null&&map.get(username)!=null) {
            System.out.println(map.get(username));
            System.out.println(token1);System.out.println(token1);
            response.getWriter().println("请不要重复提交");
        }


    }
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        doGet(request,response);
    }
}
